home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / video / xevil-1.000 / xevil-1 / main.C < prev    next >
C/C++ Source or Header  |  1995-06-01  |  3KB  |  120 lines

  1. // "main.C"
  2.  
  3. /*    Copyright (C) 1994  Steve Hardt
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 1, or (at your option)
  8.     any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.     Steve Hardt 
  20.     hardts@athena.mit.edu hardts@media.mit.edu
  21.     hardts@r4002.3dem.bioch.bcm.tmc.edu
  22.     2043 McClendon
  23.     Houston, TX 77030
  24. */
  25.  
  26. // Include Files
  27. #include "utils.h"
  28.  
  29. extern "C" {
  30. #include <sys/socket.h> 
  31. #include <sys/types.h>
  32. #include <sys/time.h>
  33.  
  34. #include <X11/Xutil.h>
  35. #include <X11/Xos.h>
  36. }
  37.  
  38. #ifdef USE_SELECT_H
  39. extern "C" {
  40. #include <sys/select.h>
  41. }
  42. #endif
  43.  
  44. #include <iostream.h>
  45.  
  46. #ifdef SELECT_NEEDS_PROTOTYPES
  47. extern "C" {
  48. int select(int,
  49.        fd_set *,
  50.        fd_set *,
  51.        fd_set *,
  52.        struct timeval *);
  53. }
  54. #endif
  55.  
  56. #include "game.h"
  57.  
  58.  
  59.  
  60. // Functions
  61. main(int argc, char **argv)
  62. {
  63. #ifdef USE_RANDOM
  64.   srandom((int)time(NULL));
  65. #else
  66.   srand((unsigned int)time(NULL));
  67. #endif
  68.  
  69.   GameP game = new Game(&argc,argv);
  70.  
  71.   assert (clock() != -1);
  72.  
  73.   long total = 0;
  74.   long events = 0;
  75.  
  76.   while (True)
  77.     {
  78.       Quanta quanta = game->get_quanta(); 
  79.       clock_t startTime = clock();
  80.  
  81.       // Clock the game
  82.       total++;
  83.  
  84.       for (int dpyNum = 0; dpyNum < game->get_dpy_max(); dpyNum++)
  85.     {
  86.       int eventsNum;
  87.       if (eventsNum = XEventsQueued(game->get_dpy(dpyNum),QueuedAfterReading))
  88.         for (int m = 0; m < eventsNum; m++)
  89.           {
  90.         XEvent event;
  91.         XNextEvent(game->get_dpy(dpyNum),&event);
  92.         game->process_event(dpyNum,&event);
  93.         events++;
  94.           }
  95.     }
  96.       
  97.       game->clock();
  98.       if (game->show_stats() && !(total % Game::REPORT_TIME))
  99.     cout << "total:" << total << " events:" << events << 
  100.       " percent:" << ((float)events / (float) total) << endl; 
  101.       
  102.  
  103.       struct timeval waitTime;
  104.       waitTime.tv_sec = 0;
  105.       clock_t diff = clock() - startTime;
  106.       if (diff > 0)
  107.     waitTime.tv_usec = 1000 * (quanta - (long)(MSEC_PER_CLOCK * diff));
  108.       else
  109.     waitTime.tv_usec = 1000 * quanta;
  110.  
  111.       if (waitTime.tv_usec > 0)
  112.     if (select(0,NULL,NULL,NULL,&waitTime) < 0)
  113.       {
  114.         cerr << "Error with select." << endl;
  115.         exit(1);
  116.       }
  117.     } 
  118. }
  119.  
  120.